package com.example.blog.cofig;

import com.example.blog.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    /*跨域配置*/
    //跨域配置，不可设置为*，不安全, 前后端分离项目，可能域名不一致
    //本地测试 端口不一致 也算跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /*允许localhost:8080这个端口访问*/
        registry.addMapping("/**")
                .allowedOrigins("http://8.136.84.238:8080")
                .allowedOrigins("http://suqiqaq.cn:8080")
                .allowedOrigins("http://localhost:8080");
    }

    /*新增登录拦截器*/
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/comments/create/change")
                .addPathPatterns("/articles/publish")
                .excludePathPatterns("/login")
                .excludePathPatterns("/register");
    }
}
